<?php

class Registry
{
    private static $_registry = array();

    public static function get($key)
    {
        if (isset(self::$_registry[$key])) {
            return self::$_registry[$key];
        }
        return null;
    }
    public static function registries()
    {
        return self::$_registry;
    }
    public static function register($key, $value)
    {
        if (isset(self::$_registry[$key])) {
            throw new \Exception('Registry "'.$key.'" already exists');
        }
        self::$_registry[$key] = $value;
        return $value;
    }
    public static function unregister($key)
    {
        if (isset(self::$_registry[$key])) {
            unset(self::$_registry[$key]);
        }
    }
    public static function getSingleton($namespace)
    {
        $key = strtolower(str_replace('\\', '_', trim($namespace, '\\')));
        if(self::get($key))
            return self::get($key);
        $object = new $namespace();
        self::register($key, $object);
        return self::get($key);
    }
}